$(function() {
    //获取token
    var token = localStorage.token;
    //加载收藏列表
    function loadCollectionList() {
        $.ajax({
            type: "get",
            url: domain.test + "/getcollection",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("token",token)
            },
            success: function (response) {
                if(response.data.rtnCode===200) {
                    var collectionList = response.data.data;
                    console.log(collectionList);
                    $(".productList").html(template("productList",{collectionList:collectionList}));
                    pull.pullDownSuccess(); //刷新成功
                }
                if(response.data.rtnCode===401) {
                    var outTimeCode = response.data.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    }
    loadCollectionList();
    var pull = new Pull($(".productList"),{
        onPullDown: function () {
            loadCollectionList();
        }
    });
    //删除收藏商品
    $(".productList").on("click",".delIcon",function() {
        var parentDom = $(this).parent().parent();
        var pId = parseInt(parentDom.attr("data-loveId"));
        console.log(pId);
        parentDom.find(".delBtn").toggle();
        parentDom.find(".delBtn").on("click",function() {
            $.ajax({
                type: "delete",
                url: domain.test + "/delcollection?id=" + pId,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("token",token);
                },
                success: function (response) {
                    console.log(response);
                    if(response.data.rtnCode===200) {
                       location.reload();
                    }
                }
            });
        });
    });
});